package artid.com.arttogo.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.DeleteAdapter;
import artid.com.arttogo.bean.QrHisItem;
import artid.com.arttogo.utils.MySharedPreferences;

/**
 * 扫码记录
 * @author
 *
 */

public class QRCodeHistoryActivity extends BaseActivity implements OnClickListener,OnItemClickListener {

	private Toast mToast;
	private ListView listviewDelete;
	private DeleteAdapter adapter;
	ArrayList<QrHisItem> listDatas;
	MySharedPreferences share;
	JSONObject objectall;
	JSONArray jsarray;
	//private LinearLayout noDataLL;
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_history);


		listDatas = new ArrayList<QrHisItem>();
		share = MySharedPreferences.getMySharedPreferences(QRCodeHistoryActivity.this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("扫码记录");
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		//noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		listviewDelete = (ListView) findViewById(R.id.history_list);
		String strJSAll = share.getValue("his", "");
		if (!TextUtils.isEmpty(strJSAll)) {
			//noDataLL.setVisibility(View.GONE);
			try {
				 objectall = new JSONObject(strJSAll);
				 jsarray = objectall.getJSONArray("data");
				for (int i = 0; i < jsarray.length(); i++) {
					JSONObject jsitem =  jsarray.getJSONObject(i);
					QrHisItem item = new QrHisItem();
					item.setTitle(jsitem.optString("title"));
					item.setTime(jsitem.optString("time"));
					item.setAuther(jsitem.optString("auther"));
					item.setUrl(jsitem.optString("code"));
					listDatas.add(item);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
//		
//			ArrayList<QrHisItem>  listOrdered = new ArrayList<QrHisItem>();
//			for (int i = listDatas.size()-1; i >=0; i--) {
//				listOrdered.add(listDatas.get(i));
//			}
//			listDatas = listOrdered;
			adapter = new DeleteAdapter(QRCodeHistoryActivity.this, listDatas);

			listviewDelete.setAdapter(adapter);
			listviewDelete.setOnItemClickListener((OnItemClickListener) this);
			listviewDelete.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
											   final int arg2, long arg3) {
					// TODO Auto-generated method stub
					AlertDialog alertDialog = new AlertDialog.Builder(QRCodeHistoryActivity.this)
					.setTitle("提示").setMessage("是否删除该记录?")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@SuppressLint("NewApi") @Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub
							listDatas.remove(arg2);
							adapter.setListData(listDatas);
							adapter.notifyDataSetChanged();
							jsarray.remove(arg2);
							objectall = new JSONObject();
							try {
								objectall.put("data", jsarray);
								share.setValue("his", objectall.toString());
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}).setNegativeButton("取消", null).show();
					return true;
				}
			});

		}else{
			//noDataLL.setVisibility(View.VISIBLE);
		}


	}



	public void showInfo(String text) {
		if (mToast == null) {
			mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		} else {
			mToast.setText(text);
			mToast.setDuration(Toast.LENGTH_SHORT);
		}
		mToast.show();
	}




	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {


		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub                                                     
		super.onDestroy();


	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

		String qrCode = listDatas.get(arg2).getUrl();
		Intent intent = new Intent(QRCodeHistoryActivity.this,ScanCertiActivity.class);
		intent.putExtra("code", qrCode);
		startActivity(intent);
//		System.out.println("输出的二维码="+qrCode);


	}


}
